package com.example.zcaxe.androidGuidePractice;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    MainActivity mActivity = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onButton_1Click(View view) {
        Toast.makeText(mActivity, "按按钮", Toast.LENGTH_SHORT).show();
        if (ContextCompat.checkSelfPermission(mActivity,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.CAMERA)) {
                Toast.makeText(mActivity, "授权", Toast.LENGTH_LONG).show();
            } else {
                ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA}, 0);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 0: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    Toast.makeText(mActivity, "成功", Toast.LENGTH_LONG).show();
                else {
                    Toast.makeText(mActivity, "失败", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
